/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.guis.resourcePicker;

import java.util.List;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.customWidgets.SearchBox;
import org.tbjd.client.dndControllers.resourcePicker.ResourcePickerDragController;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.guis.resourcePicker.items.ResourcePickerItem;
import org.tbjd.client.images.resourcePicker.menu.MenuImageBundle;
import org.tbjd.client.interfaces.Decisionmaker;
import org.tbjd.client.interfaces.Displayer;
import org.tbjd.client.interfaces.HasDragController;
import org.tbjd.client.interfaces.HasMediator;
import org.tbjd.client.interfaces.Observer;
import org.tbjd.client.listeners.resourcePicker.ResourcePickerAddListener;
import org.tbjd.client.listeners.resourcePicker.ResourcePickerAssignListener;
import org.tbjd.client.mediators.AbstractMediator;

import com.allen_sauer.gwt.dnd.client.DragController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * <p>
 * A visual list of available {@link org.tbjd.client.beans.Resource Resources}
 * </p>
 * <p>
 * Contains {@link org.tbjd.client.guis.resourcePicker.items.ResourcePickerItem ResourcePickerItem}
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class ResourcePickerGui extends Composite implements Observer, Displayer, HasMediator, Decisionmaker, HasDragController {

	static private ResourcePickerGui instance = null;
	private VerticalPanel container = null;
	private VerticalPanel resourcePickerPanel = null;

	private List<ResourcePickerItem> lstResourcePickerItems = null;
	private MenuImageBundle menuImageBundle = (MenuImageBundle) GWT.create(MenuImageBundle.class);

	private AbstractMediator mediator = null;
	private ResourcePickerDragController resourcePickerDragController = null;

	private ResourcePickerGui() {
		build();
		initWidget(this.container);
	}

	private void build() {
		this.container = new VerticalPanel();
		this.container.setStyleName("tbjd-resourcePicker");

		this.resourcePickerPanel = new VerticalPanel();
		this.resourcePickerPanel.setStyleName("tbjd-resourcePicker_itemsPanel");

		Panel menu = getMenu();
		this.container.add(menu);
		this.container.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
		this.container.add(this.resourcePickerPanel);
	}

	static public ResourcePickerGui getInstance() {
		if (instance == null) {
			instance = new ResourcePickerGui();
		}
		return instance;
	}

	public void display() {
		System.out.println("ResourcePickerGui.display() ");

		this.resourcePickerPanel.clear();

		for (ResourcePickerItem eachItem : lstResourcePickerItems) {
			this.resourcePickerPanel.add(eachItem);
			this.resourcePickerDragController.makeDraggable(eachItem, eachItem.getHandle());
		}
	}

	/**
	 * Contains :
	 * <li>Assign button</li>
	 * <li>Add button</li>
	 * <li>Search panel</li>
	 * @return com.google.gwt.user.client.ui.Panel
	 */
	private Panel getMenu() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("tbjd-resourcePicker_menu");

		PushButton addResourceButton = new PushButton(menuImageBundle.plus_bw_16x16().createImage(), menuImageBundle.plus_color_16x16().createImage());
		addResourceButton.addClickListener(new ResourcePickerAddListener());

		PushButton assignButton = new PushButton(menuImageBundle.arrow_down_bw_16x16().createImage(), menuImageBundle.arrow_down_color_16x16()
				.createImage());
		assignButton.addClickListener(new ResourcePickerAssignListener());

		horizontalPanel.add(assignButton);
		horizontalPanel.setCellVerticalAlignment(assignButton, HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setCellHorizontalAlignment(assignButton, HasHorizontalAlignment.ALIGN_LEFT);
		horizontalPanel.add(addResourceButton);
		horizontalPanel.setCellVerticalAlignment(addResourceButton, HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setCellHorizontalAlignment(addResourceButton, HasHorizontalAlignment.ALIGN_LEFT);

		SearchBox searchBox = new SearchBox();
		searchBox.setMediator(this.mediator);

		horizontalPanel.add(searchBox);
		horizontalPanel.setCellHorizontalAlignment(searchBox, HasHorizontalAlignment.ALIGN_RIGHT);

		return horizontalPanel;
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
		this.mediator.addObserver(this);
	}

	public void setDragController(DragController dragController) {
		this.resourcePickerDragController = (ResourcePickerDragController) dragController;
	}

	public void decide(Object action) {
		System.out.println("ResourcePickerGui.decide()");
		try {
			delegate(action);
		} catch (NoUpperDecisionmakerFoundException e) {
			e.printStackTrace();
		} catch (NoUpperMediatorFoundException e) {
			e.printStackTrace();
		}
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
		System.out.println("ResourcePickerGui.delegate()");
		try {
			this.mediator.decide(action);
		} catch (TBJDException e) {
			e.printStackTrace();
		}
	}

	public void handle(Object action) {
	}

	public void update(Object sender) {
		if (((Map<?, ?>) sender).containsKey(NotificationsConstants.RESOURCEPICKERITEMS_DECORATED_old)) {
			System.out.println("ResourcePickerGui.update() RESOURCEPICKERITEMS_DECORATED");
			this.lstResourcePickerItems = (List<ResourcePickerItem>) ((Map<Integer, ?>) sender).get(NotificationsConstants.RESOURCEPICKERITEMS_DECORATED);
			display();
		}
	}

	public void display(List<?> updatedBeans) {
		// TODO Auto-generated method stub
		
	}

}
